<?php
namespace BoxLogin\Controller;

use Think\Controller;

class IndexController extends \HomeController
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {

        $this->display();
    }


    /*****微信获得code返回backUrl***/
    /********/
    public function getCode()
    {
        $store_id = I('store_id', "1", "intval");
        $storeInfo = BoxModel("addon_wx_config")->where(array('store_id'=>$store_id))->find();
								//dump( $this->storeInfo);exit();
        $this->appID = $storeInfo['appID'];
        $scope = I('scope', "snsapi_base");
        $backUl = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        if ($code = I('code')) {//获得code之后跳转
            $backUrl = I('backUrl');
            if (strpos($backUrl, "?")) {
                $backUrl = $backUrl . "&code=" . $code;
            } else {
                $backUrl = $backUrl . "?code=" . $code;
            }
            header("location:$backUrl");
            exit();
        } else {//去获得code
            $bool = strpos($backUl, "?");
            $backUl = $bool === false ? ($backUl . "?backUrl=" . I('backUrl')) : ($backUl . "&backUrl=" . I('backUrl'));
            $backUl = urlencode($backUl);
            //$backUl=I('backUrl',$backUl); //登录之后回调地址
            /*******如果在微信中浏览直接使用微信同步登录*********/
            $scope = $scope ? $scope : "snsapi_base";
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appID}&redirect_uri={$backUl}&response_type=code&scope={$scope}&state=123#wechat_redirect";
            header("location:$url");
        }
    }


}
